package com.itheima.socket.udp.demo02;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * 不断的去接收我们发送端所传递过来的数据
 */
public class ReceiveDemo01 {

    public static void main(String[] args) throws Exception {

        // 1. 创建DatagramSocket对象
        DatagramSocket datagramSocket = new DatagramSocket(9999) ;

        while(true) {

            // 2. 创建数据包对象
            byte[] bytes = new byte[1024] ;
            int length = bytes.length ;
            DatagramPacket datagramPacket = new DatagramPacket(bytes , length) ;

            // 3. 接收数据
            datagramSocket.receive(datagramPacket) ;

            // 4. 解析数据
            byte[] data = datagramPacket.getData();
            int len = datagramPacket.getLength();
            System.out.println(new String(data , 0 , len , "UTF-8"));

        }

    }

}
